package cn.kgc.jihe.Work01;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class StudentList {
    //用来保存学生对象
    private static List<Student> studentList = new ArrayList<Student>();
    //新增学生
    public static boolean addStudent(Student student) {
        for (int index = 0; index < studentList.size(); index++) {
            Student stu = studentList.get(index);
            if (stu.getStuNo() == student.getStuNo()) {
                return false;
            }
        }
        return studentList.add(student);
    }
    //根据学号(唯一性)删除集合中指定的学生对象
    public static boolean delStudentById(int stuNo){
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            if(stuNo== student.getStuNo()){
                index = i;
                break;
            }
        }
        if (index != -1) {
            studentList.remove(index);
            return true;
        } else {
            return false;
        }
    }
    //根据学号修改学生信息
    public static boolean updStudentById(Student student) {
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            Student stu = studentList.get(i);
            if (stu.getStuNo()== student.getStuNo()) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            studentList.set(index, student);
            return true;
        } else {
            return false;
        }
    }
    //返回所有的学生对象
    public static List<Student> getAllStudent() {
        return studentList;
    }
    //应用程序入口方法
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        boolean flag=true;
        do {
            System.out.println("===学生信息管理系统===");
            System.out.println("1.增加学生信息");
            System.out.println("2.修改学生信息");
            System.out.println("3.删除学生信息");
            System.out.println("4.显示所有学生");
            System.out.println("0.退出");
            System.out.println("请选择：");
            int choice=input.nextInt();
            switch (choice){
                case 1:
                    System.out.println("请输入学号：");
                    int stuNo=input.nextInt();
                    System.out.println("请输入姓名：");
                    String name=input.next();
                    System.out.println("请输入年龄：");
                    int age=input.nextInt();
                    System.out.println("请输入身份证号：");
                    String idNumber=input.next();
                    Student student=new Student(stuNo,age,name,idNumber);
                    boolean result=addStudent(student);
                    if(result){
                        System.out.println("新增学生信息成功！");
                    }else {
                        System.out.println("新增学生信息失败！");
                    }
                    break;
                case 2:
                    System.out.println("请输入学号:");
                    stuNo= input.nextInt();
                    System.out.println("请输入姓名:");
                    name = input.next();
                    System.out.println("请输入年龄:");
                    age = input.nextInt();
                    System.out.println("请输入身份证号:");
                    idNumber = input.next();
                    Student student1=new Student(stuNo,age,name,idNumber);
                    updStudentById(student1);
                    System.out.println("修改成功");
                    break;
                case 3:
                    System.out.println("请输入学号：");
                    stuNo= input.nextInt();
                    delStudentById(stuNo);
                    System.out.println("删除成功");
                    break;
                case 4:
                    List<Student> students = getAllStudent();
                    Iterator<Student> a = students.iterator();
                    System.out.println("学号\t\t姓名\t\t年龄\t\t身份证号");
                    System.out.println("-----------------------------------------");
                    while(((Iterator) a).hasNext()){
                        Student stu = a.next();
                        System.out.println(stu.getStuNo() + "\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getIdNumber());
                        System.out.println("-----------------------------------------");
                    }
                    break;
                case 0:
                    System.out.println("谢谢使用本系统，欢迎下次光临！");
                    flag = false;
                    break;
                default:
                    System.out.println("你输入的序号无效，请重新输入");
                    flag = true;
                    break;

            }
        }while (true);


    }


}